package leetcode_acm.backtrack;

import java.util.LinkedList;
import java.util.List;

/**
 * 功能描述
 *
 * @author: 张庭杰
 * @date: 2023年03月22日 20:09
 */
public class CombinationSum3 {
    private List<List<Integer>> list;
    public List<List<Integer>> combinationSum3(int k, int n) {
        backtrack(k,n,0,1,new LinkedList<>());
        return list;
    }
    private void backtrack(int k, int sum, int pathSize, int startIndex, LinkedList<Integer> path){
        if(pathSize == k){
            if(sum == 0){
                list.add(new LinkedList<>(path));
            }
            return;
        }
        int bound = 10-k+pathSize;
        for(int i = startIndex; i <= bound;i++){
            if(sum - i < 0){
                return;
            }
            //做出选择
            path.addLast(i);
            //递归回溯
            backtrack(k,sum -i ,pathSize+1,i+1,path);
            path.removeLast();
        }
    }
}
